/*  Copyright (c) 2012 William Rogers and James Boud

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.Windows.Forms;
using System.Drawing;

namespace JBBRXG11
{
    public class GameWindow
    {
        NoPaintingForm the_window;
        bool settofullscreen;
        int screenwidth, screenheight;  // records size of screen - only defined in full screen display mode

        /// <summary>
        /// This is just a flag - doesn't change window in any way
        /// </summary>
        internal bool SetToFullScreen { get { return settofullscreen; } set { settofullscreen = value; } }
        internal int ScreenWidth { set { screenwidth = value; } }
        internal int ScreenHeight { set { screenheight = value; } }

        internal protected event EventHandler Window_Close_Clicked;

        // debug only
        internal System.Drawing.Graphics FormGraphics { get { return the_window.CreateGraphics(); } }

        public GameWindow()
        {
            the_window = new NoPaintingForm();
            //the_window.MaximizeBox = false;
            //the_window.MinimizeBox = false;
            the_window.Text = "JBBRXG11";
            the_window.FormClosing += new FormClosingEventHandler(the_window_FormClosing);
            settofullscreen = false;
            screenwidth = screenheight = 0;
        }

        internal void SetClientSize(int width, int height)
        {
            // Use with care - this may not interact well with full screen mode
            the_window.Width = the_window.Width - the_window.ClientSize.Width + width;
            the_window.Height = the_window.Height - the_window.ClientSize.Height + height;
        }

        void the_window_FormClosing(object sender, FormClosingEventArgs e)
        {
            // This routine is called when the user clicks the x in the top right
            // corner of the window.  It just notifies the game that close has been
            // requested and cancels the close window operation
            Window_Close_Clicked(this, null);
            e.Cancel = true;
        }

        internal void Show()
        {
            the_window.Show();
        }

        internal void Close()
        {
            // The window may already be closed, but calling close
            // again doesn't seem to cause any trouble
            the_window.Close();
        }

        public bool AllowUserResizing
        {
            // TODO:  Should be ready to remake graphics device etc when dimensions change
            get
            {
                return the_window.FormBorderStyle == FormBorderStyle.Sizable;
            }
            set
            {
                if (value)
                    the_window.FormBorderStyle = FormBorderStyle.Sizable;
                else
                    the_window.FormBorderStyle = FormBorderStyle.Fixed3D;
            }
        }

        public Microsoft.Xna.Framework.Rectangle ClientBounds
        {
            get
            {
                if (settofullscreen)
                    return new Microsoft.Xna.Framework.Rectangle(0, 0, screenwidth, screenheight);
                else
                    return new Microsoft.Xna.Framework.Rectangle(0, 0, the_window.ClientRectangle.Width, the_window.ClientRectangle.Height);
            }
        }

        public IntPtr Handle { get { return the_window.Handle; } }

        // Summary:
        //     Gets the device name of the screen the window is currently in.
        public string ScreenDeviceName { get { return "Not Yet Implemented"; } }

        public string Title { get { return the_window.Text; } set { the_window.Text = value; } }

        internal bool MouseVisible { get { return the_window.MouseVisible; } set { the_window.MouseVisible = value; } }

        public event EventHandler ClientSizeChanged;  // not yet implemented
        public event EventHandler ScreenDeviceNameChanged;  // not yet implemented

        public void BeginScreenDeviceChange(bool willBeFullScreen)
        {
            throw new NotImplementedException("JBBRXG11.GameWindow.BeginScreenDeviceChange");
        }

        public void EndScreenDeviceChange(string screenDeviceName)
        {
            throw new NotImplementedException("JBBRXG11.GameWindow.EndScreenDeviceChange");
        }

        public void EndScreenDeviceChange(string screenDeviceName, int clientWidth, int clientHeight)
        {
            throw new NotImplementedException("JBBRXG11.GameWindow.EndScreenDeviceChange");
        }

        protected void OnActivated()
        {
            throw new NotImplementedException("JBBRXG11.GameWindow.OnActivated");
        }

        protected void OnClientSizeChanged()
        {
            throw new NotImplementedException("JBBRXG11.GameWindow.OnActivated");
        }

        protected void OnDeactivated()
        {
            throw new NotImplementedException("JBBRXG11.GameWindow.OnActivated");
        }

        protected void OnPaint()
        {
            throw new NotImplementedException("JBBRXG11.GameWindow.OnPaint");
        }

        protected void OnScreenDeviceNameChanged()
        {
            throw new NotImplementedException("JBBRXG11.GameWindow.OnScreenDeviceNameChanged");
        }

        protected void SetTitle(string title)
        {
            the_window.Text = title;
        }
    }

    class NoPaintingForm : Form
    {
        bool mouse_visible;

        public NoPaintingForm()
        {
            DoubleBuffered = false;
            mouse_visible = true;
        }

        internal bool MouseVisible
        {
            get
            {
                return mouse_visible;
            }
            set
            {
                mouse_visible = value;
                if (mouse_visible) Cursor.Show(); else Cursor.Hide();
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //e.Graphics.FillRectangle(Brushes.Pink, ClientRectangle);
            MouseVisible = MouseVisible;  // force update
        }
    }
}
